package com.ebeitech.library.util.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.ebeitech.library.constant.SPConstants;
import com.ebeitech.library.util.Log;

/**
 * Created by alice on 2017/10/31.
 */
public class StatusBarCompat {

	public static final boolean IS_LIGHT_STATUS = false;
	private static final int COLOR_DEFAULT = Color.rgb(105, 178, 252);

	public static void compat(Activity activity, int statusColor) {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
			setTranslucentStatus(activity, true);
			SystemBarTintManager tintManager = new SystemBarTintManager(activity);
			tintManager.setStatusBarTintEnabled(true);
			// 此处可以重新指定状态栏颜色
			tintManager.setStatusBarTintColor(statusColor);
		}

		fitsSystemWindows(activity);
	}

	public static void compat(Activity activity) {
		compat(activity, COLOR_DEFAULT);
	}

	public static void fitsSystemWindows(Activity activity) {
		if (activity.getWindow().getDecorView() != null) {
			View view = ((ViewGroup) activity.findViewById(android.R.id.content)).getChildAt(0);
			if (view instanceof ViewGroup) {
				ViewGroup viewGroup = (ViewGroup) view;
				viewGroup.setFitsSystemWindows(true);
				viewGroup.setClipToPadding(true);
			}
		}
	}

	public static void fitsSystemWindows(Activity activity, boolean isStatusVisible) {
		if (!isStatusVisible) {
			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
				setTranslucentStatus(activity, false);
				SystemBarTintManager tintManager = new SystemBarTintManager(activity);
				tintManager.setStatusBarTintEnabled(false);
			}
		}
		if (activity.getWindow().getDecorView() != null) {
			View view = ((ViewGroup) activity.findViewById(android.R.id.content)).getChildAt(0);
			if (view instanceof ViewGroup) {
				ViewGroup viewGroup = (ViewGroup) view;
				viewGroup.setFitsSystemWindows(false);
				viewGroup.setClipToPadding(false);
			}
		}
	}

	public static int getStatusBarHeight(Context context) {
		int result = 0;
		int resourceId = context.getResources().getIdentifier(
				"status_bar_height", "dimen", "android");
		if (resourceId > 0) {
			result = context.getResources().getDimensionPixelSize(resourceId);
		}
		return result;
	}

	private static void setTranslucentStatus(Activity activity, boolean on) {
		Window win = activity.getWindow();
		WindowManager.LayoutParams winParams = win.getAttributes();
		final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
		if (on) {
			winParams.flags |= bits;
		} else {
			winParams.flags &= ~bits;
		}
		win.setAttributes(winParams);
	}

	public static boolean setStatusBar(Activity activity, Bundle arg0) {
		boolean isFitStatusBar = true;
		boolean isLightStatusBar = IS_LIGHT_STATUS;
		if (arg0 != null) {
			isFitStatusBar = arg0.getBoolean(SPConstants.IS_FIT_STATUS_BAR, true);
			isLightStatusBar = arg0.getBoolean(SPConstants.IS_LIGHT_STATUS_BAR, isLightStatusBar);
		}

		if (isLightStatusBar) {
			switch (RomUtils.getLightStatausBarAvailableRomType()) {
				case RomUtils.AvailableRomType.MIUI:
					Log.i("当前系统为MIUI6或以上 浅色状态栏可用");
					break;

				case RomUtils.AvailableRomType.FLYME:
					Log.i("当前系统为Flyme4或以上 浅色状态栏可用");
					break;

				case RomUtils.AvailableRomType.ANDROID_NATIVE:
					Log.i("当前系统为Android M或以上 浅色状态栏可用");
					break;

				case RomUtils.AvailableRomType.NA:
					Log.i("当前系统浅色状态栏不可用");
					isFitStatusBar = false;
					break;
			}

			if (isFitStatusBar) {
				LightStatusBarUtils.setLightStatusBar(activity, true);
			}
		}
		return isFitStatusBar;
	}

}
